package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Permission;
import cn.wolfcode.ssm.mapper.PermissionMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class PermissonServiceImpl implements IPermissionService {


    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Override
    public void deleteById(Long id) {
        if(id != null){
            permissionMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);

    }

    @Override
    public Permission selectById(Long id) {
        if(id != null){
            return permissionMapper.selectByPrimaryKey(id);
        }
        return null;
    }



    @Override
    public void updateById(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }


    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        //开始分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //拦截对应的sql，加上LIMIT字句
        List<Permission> permissions = permissionMapper.selectAll();
        //自动封装（只需要提供List参数）
        return new PageInfo<>(permissions);


    }

    @Override
    public void reload() {
        //先查出数据库中原本有的权限数据（String类型）
        List<String> allExpression = permissionMapper.listAllExpression();
        //通过RequestMappingHandlerMapping获取HandlerMapping
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> methods = handlerMethods.values();
        //获取方法methods
        for (HandlerMethod method : methods) {
            //判断是否有自定义RequireAnnotation注解
            RequireAnnotation methodAnnotation = method.getMethodAnnotation(RequireAnnotation.class);
            if( methodAnnotation == null){
                continue;//空则跳过
            }
            //每次循环都需要去重(判断表达式集合是否包含被遍历的表达式)
            if (allExpression.contains(methodAnnotation.expression())) {
                continue;//包含则跳过
            }
            //往数据库中添加权限数据
             Permission permission = new Permission();
             permission.setName(methodAnnotation.name());
             permission.setExpression(methodAnnotation.expression());
             //将该权限数据放在集合中（避免重复添加）
             allExpression.add(methodAnnotation.expression());
            permissionMapper.insert(permission);

        }

    }

    @Override
    public List<String> selectForExpressionById(Long empId) {
        return permissionMapper.selectForExpressionById(empId);
    }
}
